﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Security;
using danreng.Application;
using danreng.Domain;
using sharp_net;
using sharp_net.Repositories;

namespace danreng.Api.Controllers {
    public class ItemController : ApiController {

        public IItemService itemApp { get; set; }

        public ItemController(IItemService service) {
            this.itemApp = service;
        }

        // GET api/item/xxxxx
        public ItemData Get(string id) {
            ItemData result = itemApp.Get(id);
            return result;
        }

        // GET api/item?page=1
        public IEnumerable<ItemData> Get(int page = 1) {
            int cout = 0;
            IEnumerable<ItemData> result = itemApp.GetListPager(ref cout, 10, page);
            return result;
        }

        // POST api/item
        [Authorize]
        public ItemData Post(ItemData data) {
            ItemData item = itemApp.Creat(data, User.Identity.Name);
            return item;
        }

        // PUT api/item/5
        [Authorize]
        public void Put(ItemData data) {
            itemApp.Update(data);
        }

        // DELETE api/item/5
        [Authorize]
        public void Delete(string id) {
            ItemData item = new ItemData {
                Id = id,
                ActEnum = (int)eAct.Delete
            };
            itemApp.Update(item);
        }
    }
}